package com.xhh.nphm.gaoxin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xhh.nphm.common.constant.Constants;
import com.xhh.nphm.common.utils.AcquireWFNoUtils;
import com.xhh.nphm.gaoxin.entity.GxqTtelGetDealEty;
import com.xhh.nphm.gaoxin.mapper.GxqTelGetDealDao;
import com.xhh.nphm.gaoxin.service.GxqITelGetDealService;
import com.xhh.nphm.system.domain.*;
import com.xhh.nphm.system.mapper.SortMapper;
import com.xhh.nphm.system.service.IEventAddressService;
import com.xhh.nphm.system.service.ISensitiveInfoService;
import com.xhh.nphm.system.service.ITworkFormLogService;
import com.xhh.nphm.system.service.impl.CommonServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class GxqTelGetDealServiceImpl extends ServiceImpl<com.xhh.nphm.gaoxin.mapper.GxqTelGetDealDao, GxqTtelGetDealEty> implements GxqITelGetDealService {
    @Autowired
    private GxqTelGetDealDao GxqTelGetDealDao;
    @Autowired
    private CommonServiceImpl commonService;
    @Autowired
    private ISensitiveInfoService sensitiveInfoService;
    @Autowired
    private IEventAddressService eventAddressService;
    @Autowired
    private ITworkFormLogService tworkFormLogService;
    @Autowired
    private SortMapper sortMapper;
    @Autowired
    private AcquireWFNoUtils acquireWFNoUtils;
    /**
     * 查询来电记录草稿列表
     *
     * @param GxqTtelGetDealEty
     * @return
     */
    @Override
    public List<GxqTtelGetDealEty> listDraft(GxqTtelGetDealEty GxqTtelGetDealEty) {
        return GxqTelGetDealDao.listDraft(GxqTtelGetDealEty);
    }

    /**
     * 查询待处理来电记录列表
     *
     * @param userNo 当前登录账号
     * @param carrierType 运营商标识
     * @param GxqTtelGetDealEty 查询条件
     * @return 列表数据
     */
    @Override
    public List<GxqTtelGetDealEty> listWait(String userNo,Integer carrierType,GxqTtelGetDealEty GxqTtelGetDealEty) {
        return GxqTelGetDealDao.listWait(userNo,carrierType,GxqTtelGetDealEty);
    }


    /**
     * 新增 来电记录草稿
     * @param GxqTtelGetDealEty
     * @return
     */
    @Override
    public int insert(GxqTtelGetDealEty GxqTtelGetDealEty, EventAddress eventAddress, SensitiveInfo sensitiveInfo) {
        String tmp = "";
        tmp = commonService.makeRandomStr(6);
        GxqTtelGetDealEty.setWsKey(tmp);
        if(acquireWFNoUtils.isEnable()){
            String telWsCode = acquireWFNoUtils.getTelWsCode();
            if(Objects.equals(telWsCode,"")){
                tmp = commonService.byTelGetSN(5);
                acquireWFNoUtils.renewValue(Constants.TEL_CODE_COUNT_KEY);
            }else{
                tmp = telWsCode;
            }
        }else{
            tmp = commonService.byTelGetSN(5);//流水号
        }
        GxqTtelGetDealEty.setWsCode(tmp);
        Date fromTime = GxqTtelGetDealEty.getFromTime();
        Date date = new Date();

        GxqTtelGetDealEty.setLiftLength((int) (date.getTime() -fromTime.getTime()) / 1000);
        GxqTtelGetDealEty.setLiftEnd(date);
        // 保存电话记录
        int insert = GxqTelGetDealDao.insert(GxqTtelGetDealEty);
        // 更新敏感信息
        sensitiveInfo.setTelGetDealPk(GxqTtelGetDealEty.getPkId());
        sensitiveInfoService.updateById(sensitiveInfo);
        // 保存事发地址详细信息
        if (eventAddress !=null && eventAddress.getAddress() != null) {
            eventAddress.setTelId(GxqTtelGetDealEty.getPkId());
            eventAddressService.insert(eventAddress);
        }
        // 保存和备份电话记录操作日志
        TworkFormLogEty tworkFormLogEty = GxqTtelGetDealEty.getTworkFormLogEty();
        tworkFormLogEty.setWfId(GxqTtelGetDealEty.getPkId());
        tworkFormLogService.save(tworkFormLogEty);
        return insert;
    }

    /**
     * 新增来电记录草稿
     * @param GxqTtelGetDealEty
     * @return
     */
    @Override
    public int insert(GxqTtelGetDealEty GxqTtelGetDealEty) {
        String tmp = "";
        tmp = commonService.makeRandomStr(6);
        GxqTtelGetDealEty.setWsKey(tmp);
        if(acquireWFNoUtils.isEnable()){
            String telWsCode = acquireWFNoUtils.getTelWsCode();
            if(Objects.equals(telWsCode,"")){
                tmp = commonService.byTelGetSN(5);
                acquireWFNoUtils.renewValue(Constants.TEL_CODE_COUNT_KEY);
            }else{
                tmp = telWsCode;
            }
        }else{
            tmp = commonService.byTelGetSN(5);
        }
        GxqTtelGetDealEty.setWsCode(tmp);
        int insert = GxqTelGetDealDao.insert(GxqTtelGetDealEty);
        return insert;
    }

    /**
     * 编辑  来电记录草稿
     * @param GxqTtelGetDealEty
     * @return
     */
    @Override
    public int update(GxqTtelGetDealEty GxqTtelGetDealEty) {
        int row = 1;
        GxqTtelGetDealEty oldGxqTtelGetDealEty = GxqTelGetDealDao.selectById(GxqTtelGetDealEty.getPkId());
        if (oldGxqTtelGetDealEty != null) {
            row = GxqTelGetDealDao.updateById(GxqTtelGetDealEty);
            // 保存和备份电话记录操作日志
            TworkFormLogEty tworkFormLogEty = GxqTtelGetDealEty.getTworkFormLogEty();
            tworkFormLogService.save(tworkFormLogEty);
        }
        return row;
    }


    /**
     * 我处理的来电列表
     *
     * @param GxqTtelGetDealEty
     * @return
     */
    @Override
    public List<GxqTtelGetDealEty> listHandle(GxqTtelGetDealEty GxqTtelGetDealEty) {
        return GxqTelGetDealDao.listHandle(GxqTtelGetDealEty);
    }

    /**
     * 坐席办结来电列表
     *
     * @param GxqTtelGetDealEty
     * @return
     */
    @Override
    public List<GxqTtelGetDealEty> listFinish(GxqTtelGetDealEty GxqTtelGetDealEty) {
        return GxqTelGetDealDao.listFinish(GxqTtelGetDealEty);
    }

    /**
     * 已派工单来电列表
     *
     * @param GxqTtelGetDealEty
     * @return
     */
    @Override
    public List<GxqTtelGetDealEty> listWork(GxqTtelGetDealEty GxqTtelGetDealEty) {
        return GxqTelGetDealDao.listWork(GxqTtelGetDealEty);
    }

    /**
     * 根据 id 可以进行 多选删除 和 单个删除
     * @param ids
     * @return
     */
    @Override
    public int deleteByIds(String[] ids) {
        return GxqTelGetDealDao.deleteByIds(ids);
    }

    /**
     * 根据 pkid 查询 来电记录草稿
     * @param pkId
     * @return
     */
    @Override
    public GxqTtelGetDealEty selectByPkId(Integer pkId) {
        return GxqTelGetDealDao.selectById(pkId);
    }

    /**
     * 根据 fromTelA 查询来电历史记录
     * @return
     */
    @Override
    public List<GxqTtelGetDealEty> historyLiaison(GxqTtelGetDealEty GxqTtelGetDealEty) {
        return GxqTelGetDealDao.historyLiaison(GxqTtelGetDealEty);
    }

    /**
     * 查询我接听的来电列表
     *
     * @param GxqTtelGetDealEty
     * @return
     */
    @Override
    public List<GxqTtelGetDealEty> myGetList(GxqTtelGetDealEty GxqTtelGetDealEty) {
        return GxqTelGetDealDao.myGetList(GxqTtelGetDealEty);
    }

    @Override
    public List<GxqTtelGetDealEty> listPending(GxqTtelGetDealEty GxqTtelGetDealEty) {
        return GxqTelGetDealDao.listPending(GxqTtelGetDealEty);
    }


    /**
     * 查询待值班长处理列表
     * @param GxqTtelGetDealEty
     * @return
     */
    public List<GxqTtelGetDealEty> listMtrWait(GxqTtelGetDealEty GxqTtelGetDealEty) {
        return GxqTelGetDealDao.listMtrWait(GxqTtelGetDealEty);
    }

    /**
     * 查询值班长退回列表
     * @param GxqTtelGetDealEty
     * @return
     */
    public List<GxqTtelGetDealEty> listMtrBack(GxqTtelGetDealEty GxqTtelGetDealEty) {
        return GxqTelGetDealDao.listMtrBack(GxqTtelGetDealEty);
    }

    /**
     * 查询我办结的来电列表
     * @param GxqTtelGetDealEty
     * @return
     */
    public List<GxqTtelGetDealEty> listMyOver(GxqTtelGetDealEty GxqTtelGetDealEty) {
        return GxqTelGetDealDao.listMyOver(GxqTtelGetDealEty);
    }

    /**
     * 历史不满意数
     * @param loginName
     * @return
     */
    @Override
    public String countHistory(String loginName) {
        StringBuilder sb= new StringBuilder();
        QueryWrapper<GxqTtelGetDealEty> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("fromTelA", loginName);
        List<GxqTtelGetDealEty> telGetDeals = GxqTelGetDealDao.selectList(queryWrapper);
        sb.append(telGetDeals.size());
        sb.append("/");
        long time = 0;
        for (GxqTtelGetDealEty telGetDeal : telGetDeals) {
//            time += Math.abs((telGetDeal.getLiftEnd().getTime()-telGetDeal.getFromTime().getTime()));
            time += Math.abs(telGetDeal.getLiftLength());
        }
        double m = 0d;
        if (telGetDeals!=null&&telGetDeals.size()>0){
            long  t= time/telGetDeals.size();
             m= t/60d;
        }else{
             m = time/60d;
        }
        sb.append(String.format("%.2f",m));
        return sb.toString();
    }

    /**
     * 常涉及问题
     * @param fromTelA 当前号码
     * @return
     */
    @Override
    public String problem(String fromTelA) {
        if(fromTelA==null) return "";
        List<Map<String,String>> map= GxqTelGetDealDao.selectCountProblem(fromTelA);
        if(map==null || map.size() == 0) return "";
        BSort dalei = sortMapper.selectByCodePk(map.get(0).get("dalei"));
        BSort dalei1 = null;
        if(map.size() > 1) {
            dalei1 = sortMapper.selectByCodePk(map.get(1).get("dalei"));
        }
        StringBuilder stringBuilder = new StringBuilder();

        if(dalei!=null) stringBuilder.append(dalei.getSortname());
        stringBuilder.append(",");
        if(dalei1!=null) stringBuilder.append(dalei1.getSortname());
        return stringBuilder.toString();
    }

    /**
     * 不满意数
     * @param fromTelA
     * @return
     */
    @Override
    public String noLike(String fromTelA) {

        return GxqTelGetDealDao.noLike(fromTelA);
    }

    /**
     * 随机获取20条待处理来电记录
     * @param
     * @return
     */
    @Override
    public List<GxqTtelGetDealEty> getListWait(Integer num) {
        return GxqTelGetDealDao.getListWait(num);
    }

    /**
     * 查询未获取待处理来电记录条数
     * @param carrierType 运营商标识
     * @return
     */
    @Override
    public Integer countListWait(Integer carrierType) {
        return GxqTelGetDealDao.countListWait(carrierType);
    }


    /**
     * 查询未获取列表数据
     * @param carrierType 运营商标识
     * @param GxqTtelGetDealEty 查询条件
     * @return 列表数据
     */
    @Override
    public List<GxqTtelGetDealEty> noGetListWait(Integer carrierType,GxqTtelGetDealEty GxqTtelGetDealEty) {
        return GxqTelGetDealDao.noGetListWait(carrierType,GxqTtelGetDealEty);
    }

    /**
     * 重录件列表数据
     * @param carrierType 运营商标识
     * @param GxqTtelGetDealEty 查询条件
     * @return 列表数据
     */
    @Override
    public List<GxqTtelGetDealEty> reenterList(Integer carrierType,GxqTtelGetDealEty GxqTtelGetDealEty) {
        return GxqTelGetDealDao.reenterList(carrierType,GxqTtelGetDealEty);
    }

    /**
     * 紧急件列表数据
     * @param GxqTtelGetDealEty 查询条件
     * @return 列表数据
     */
    @Override
    public List<GxqTtelGetDealEty> urgentList(Integer carrierType,GxqTtelGetDealEty GxqTtelGetDealEty) {
        return GxqTelGetDealDao.urgentList(carrierType,GxqTtelGetDealEty);
    }

    /**
     * 释放获取件列表数据
     * @param GxqTtelGetDealEty
     * @return
     */
    @Override
    public List<GxqTtelGetDealEty> freedListWaitJson(GxqTtelGetDealEty GxqTtelGetDealEty) {
        return GxqTelGetDealDao.freedListWaitJson(GxqTtelGetDealEty);
    }

    @Override
    public List<GxqTtelGetDealEty> telGetList(GxqTtelGetDealEty GxqTtelGetDealEty) {
        return GxqTelGetDealDao.telGetList(GxqTtelGetDealEty);
    }

    /**
     * 创建工单时，更新电话记录
     * @param telRecord
     * @return
     */
    @Override
    public int whenWFCreateSet(GxqTtelGetDealEty telRecord) {
        return GxqTelGetDealDao.whenWFCreateSet(telRecord);
    }

    /**
     * 通过信件id查询信件
     * @param PkId
     * @return
     */
    @Override
    public GxqTtelGetDealEty selectByTelPkId(int PkId) {
        return GxqTelGetDealDao.selectByTelPkId(PkId);
    }

    /**
     * 关联推荐
     * @param GxqTtelGetDealEty
     * @return
     */
    @Override
    public List<GxqTtelGetDealEty> relevanceRec(GxqTtelGetDealEty GxqTtelGetDealEty) {
        String keyWord = GxqTtelGetDealEty.getKeyWord();
        List<GxqTtelGetDealEty> telGetDealList = new ArrayList<>();
        if (keyWord != null){
            String[] keyWords = keyWord.split(",");
            for (String key : keyWords) {
                GxqTtelGetDealEty.setKeyWord(key);
                telGetDealList.addAll(GxqTelGetDealDao.relevanceRec(GxqTtelGetDealEty));
            }
        }
        return telGetDealList;
    }

    @Override
    public List<GxqTtelGetDealEty> accSubjectDraw(GxqTtelGetDealEty GxqTtelGetDealEty) {
        return GxqTelGetDealDao.accSubjectDraw(GxqTtelGetDealEty);
    }

    @Override
    public List<GxqTtelGetDealEty> telHistoryByAddress(GxqTtelGetDealEty GxqTtelGetDealEty) {
        return GxqTelGetDealDao.telHistoryByAddress(GxqTtelGetDealEty);
    }
}